/*
* @Author: Ziwei Yang
* @Date:   2016-10-19
* @Last Modified by:   Ziwei Yang
* @Last Modified time: 2016-10-19
*/

'use strict';
var Terrain = function(param){
	var self = {
		x:0,
		y:0,
		height:0,
		width:0,
	};
	self.type;
	self.defined = {"name":"black","x":'black',"y":32,"bgx":"-564px","bgy":"-110px","hp":1,"undestructable":true,"collision":false,"redraw":true};
	self.img;
	if(param){
		if(param.x) self.x = param.x;
		if(param.y) self.y = param.y;
		if(param.width) self.width = param.width;
		if(param.height) self.height = param.height;
		if(param.type) self.type = param.type;
		if(param.defined) self.defined = param.defined;
	}
	self.hovered = function(mouse){
		if(mouse.x >= self.x && mouse.x < self.x + self.width && mouse.y > self.y && mouse.y <= self.y + self.height){
			return true;
		}else{
			return false;
		}
	}
	self.draw = function(){

	}
	return self;
}
Terrain.searchTarget = function(mouse,data){
	for(var i in data){
		for(var k in data[i]){
			var target = data[i][k];
			if(mouse.x >= target.x && mouse.x < target.x + target.width && mouse.y > target.y && mouse.y <= target.y + target.height){
				target.defined = terrainSelected;
				console.log(target);
			}
		}
	}
}
Terrain.renderList = function(target){
	console.log(terrainList);
	for(var i in terrainList.models){
		var model = terrainList.models[i];
		var node = document.createElement('li');
		if(model.x!=='black'){
			var style  = 'cursor:pointer;margin-right:5px;border:2px solid transparent;list-style:none;display:inline-block;height:34px;width:32px;';
				style += 'background-image: url("'+img.src+'");';
				style += 'background-repeat: no-repeat;';
				style += 'background-position:'+model.bgx+' '+model.bgy+';';
				style += 'background-size: 828px 660px;';			
		}else{
			var style  = 'cursor:pointer;margin-right:5px;border:2px solid transparent;list-style:none;display:inline-block;height:34px;width:32px;';
				style += 'background-color: ' + model.x + ';';
		}
		node.setAttribute('style',style);
		node.setAttribute('data-obj',JSON.stringify(model));
		node.setAttribute('class','model-picker');
		node.onmouseover = function(event){
			this.style.border = "2px solid red";
		}
		node.onmouseout = function(event){
			this.style.border = "2px solid transparent";
		}
		node.onclick = function(event){
			var data = strToJson(this.getAttribute('data-obj'));
			terrainSelected = data;
		}
		target.appendChild(node);
	}
}
